/**
 * 
 */
package org.ssi.web.struts.interceptor;

import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.bjtech.domain.User;
import com.bjtech.web.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

/**
 * @author 
 *
 */
public class AuthenticateInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9199462974170380770L;
	protected static final Logger LOG = LoggerFactory.getLogger(AuthenticateInterceptor.class);
	
	protected String sessionName;
	public String safeUris;

	public void setSessionName(String sessionName) {
		this.sessionName = sessionName;
	}

	
	/* 
	 * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();

        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
        HttpSession session = request.getSession();
    	Long UserID = null;
    	
		String redirect = "/ckms";
		if (UserID == null && (session.getAttribute(Constants.CKMS) != null)) {
			UserID = ((User) session.getAttribute(Constants.CKMS)).getUserId();
		}
		if (UserID == null && (session.getAttribute(Constants.MANAGEMENT) != null)) {
			UserID = ((User) session.getAttribute(Constants.MANAGEMENT)).getUserId();
		}

    	String uri = request.getRequestURI().substring(request.getContextPath().length());
    		
		if (!isValidSession(uri)&&UserID == null) {
//			if (request.getServletPath().startsWith("/manager")) {
//				redirect = request.getContextPath() + "/manager/";
//			} else if (request.getServletPath().startsWith("/ckms")) {
//				redirect = request.getContextPath() + "/ckms/";
//			} else {
				redirect = request.getContextPath() + "/index.jsp";
//			}
			request.setAttribute("error", "您还没有登录！或者会话期已过，请重新登录！&nbsp;<a href=\"" + redirect + "\">登录</a>");
			LOG.info("登陆超时...");
			RequestDispatcher rd = request.getRequestDispatcher("/commons/error-pages/error.jsp");
			rd.forward(request, response);
			return null;
		}else
		{
	        String result = invocation.invoke();   
	        
			return result;
		}
	}

	private boolean isValidSession(String uri) {
		String[] uris=safeUris.split(",");
		for(String s:uris){
			if(uri.indexOf(s)>-1){
				return true;
			}
		}
		return false;
	}

	public String getSafeUris() {
		return safeUris;
	}

	public void setSafeUris(String safeUris) {
		this.safeUris = safeUris;
	}

}
